package features.advance.jdk.agent;

import com.sun.tools.attach.AttachNotSupportedException;
import com.sun.tools.attach.VirtualMachine;

import java.io.IOException;
import java.lang.management.ManagementFactory;

/**
 * @author LIN
 * @date 2022-05-06 20:17
 */
public class JvmUtil {

    /**
     * 獲取當前JVM
     * @return
     * @throws IOException
     * @throws AttachNotSupportedException
     */
    public static VirtualMachine getVirtualMachine() throws AttachNotSupportedException, IOException
    {
        String pid = ManagementFactory.getRuntimeMXBean().getName();
        int indexOf = pid.indexOf('@');
        if (indexOf > 0) {
            pid = pid.substring(0, indexOf);
        }
        return VirtualMachine.attach(pid);
    }
}
